﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FineUI;
namespace mydddd.sysModule
{
    public partial class SetIcon : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //读取图标
                DirectoryInfo png = new DirectoryInfo(Server.MapPath("~/res/icon"));
                //asp.net2.0
                // dlist.DataSource = png.GetFiles("*.png");
                //dlist.DataBind();

                //asp.net 4.0
                dlist.DataSource = png.EnumerateFiles("*.png");
                dlist.DataBind();
            }
        }

        //设置图标
        protected void imgbtn_OnCommand(object sender, CommandEventArgs e)
        {
            string png = "res/icon/" + e.CommandArgument.ToString();
            //Alert.Show(png);
            if (Request["id"] == null)
                PageContext.RegisterStartupScript(ActiveWindow.GetWriteBackValueReference(png, "<img src='/" + png + "?" + DateTime.Now.ToString("fff") + "' />") + ActiveWindow.GetHideReference());
            else
            {
                updateIcon(png);
            }
        }

        //取消图标
        protected void btn_OnClick(object sender, EventArgs e)
        {
            if (Request["id"] == null)
                PageContext.RegisterStartupScript(ActiveWindow.GetWriteBackValueReference(string.Empty, string.Empty) +
                                                  ActiveWindow.GetHideReference());
            else
            {
                updateIcon(String.Empty);
            }
        }

        private void updateIcon(string png)
        {
            Dictionary<string, object> dic = new Dictionary<string, object>();

            dic["moduleIcon"] = png;

            Dictionary<string, object> dicWhere = new Dictionary<string, object>();

            dicWhere["id"] = int.Parse(Request["id"]);

            if (UpdateDB("sysModule", dic, dicWhere))
            {
                AlertInforNoClose("修改图标成功！");
            }
            else
            {
                Alert.Show("修改失败");
            }
        }
    }
}